//
// Created by ziya on 2021/1/17.
//

#include "../include/localVariableTable.h"

//=====
array<Slot *, 5>* LocalVariableTable::container() {
    return &_container;
}

//=====
void LocalVariableTable::storeInt(jint index, jint val) {
    _container[index] = new Slot(val);
}

void LocalVariableTable::storeInt(jint index, Slot* slot) {
    _container[index] = slot;
}

void LocalVariableTable::storeDouble(jint index, double val) {
    // 将一个double拆成两个int
    int* p = (int *)&val;

    // 将两个int封装成Slot
    // store
    _container[index] = new Slot(*p);
    _container[index + 1] = new Slot(*(p + 1));
}

void LocalVariableTable::storeDouble(jint index, Slot* slot) {
    _container[index] = slot;
}

jint LocalVariableTable::loadInt(jint index) {
    return _container[index]->getData();
}

jdouble LocalVariableTable::loadDouble(jint index) {
    int data[2];

    // 取出数据
    Slot* slot1 = _container[index];
    Slot* slot2 = _container[index + 1];

    //组合成double
    *(data) = slot1->getData();
    *(data + 1) = slot2->getData();

    return *(double *)data;
}

Slot* LocalVariableTable::getIntSlot(jint index) {
    return _container[index];
}
